package com.action;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bean.Daobean;
import com.bean.Sqlmap;
import com.bean.Struts;
import com.bean.Template;
import com.util.ConfigUtil;
import com.util.FreeMarkerHelper;
import com.util.Function;
import com.util.XMLParse;

@SuppressWarnings("serial")
public class TemplateAction extends BaseAction {
    /**
     * 模板
     */
    private String sourceFile;
    private String targetFile;
    /**
     * 模板路径
     */
    private String sourcePath;
    private String targetPath;
    
    /**
     * 模板类
     */
    private Template template;
    private List ids;
    private List beanList;
    public String toCreateTemplate(){
    	beanList = new ArrayList();
    	String beanListPath = this.getMyConfigPropertyValue("src")+"/com/bean";
    	File file = new File(beanListPath);
		if(file.exists()){
			File[] filelist = file.listFiles();
			for(int i=0;i<filelist.length;i++){
				File fileobject = filelist[i];
				String filename = fileobject.getName();
				if(filename!=null && filename.length()>1){
					filename = filename.substring(0, 1).toLowerCase()+filename.substring(1);
					filename  = filename.replaceAll(".java", "");
					beanList.add(filename);
				}
			}
		}
        return SUCCESS;
    }

    @SuppressWarnings("unchecked")
	public String createTemplate(){
    	String action="";
    	String struts_xml="";
    	String service="";
    	String dao="";
    	String serviceimp="";
    	String daoimp="";
    	String sqlmap_xml="";
    	//生成后台JSP页面
    	/**
    	 * 添加页面
    	 */
    	String add_jsp="";
    	/**
    	 * 修改页面
    	 */
    	String edit_jsp="";
    	/**
    	 * 列表页面
    	 */
    	String list_jsp="";
    	String daobeanXml="";
    	if(ids!=null){
    		int SIZE = ids.size();
    		for(int i=0;i<SIZE;i++){
    			String id = ids.get(i).toString();
    			if(id.equals("1")){
    				action = id;
    			}
    			else if(id.equals("2")){
    				struts_xml = id;
    			}
    			else if(id.equals("3")){
    				service = id;
    			}
    			else if(id.equals("4")){
    				dao = id;
    			}
    			else if(id.equals("5")){
    				serviceimp = id;
    			}
    			else if(id.equals("6")){
    				daoimp = id;
    			}
    			else if(id.equals("7")){
    				sqlmap_xml = id;
    			}
    			else if(id.equals("8")){
    				add_jsp = id;
    			}
    			else if(id.equals("9")){
    				edit_jsp = id;
    			}
    			else if(id.equals("10")){
    				list_jsp = id;
    			}
    			else if(id.equals("11")){
    				daobeanXml = id;
    			}
    		}
    		
    	}
    	
    	if(template.getObjectName()==null || template.getObjectName().trim().equals("")){
    		Function.alertAndBack("Bean对象名必须填写!", -1, this.getRequest(), this.getResponse(),
    				null);
    	}
    	
    	
    	
//    	首字母大写的:如：User
    	String BeanName = (new StringBuilder()).append(Character.toUpperCase(template.getObjectName().trim().charAt(0))).append(template.getObjectName().trim().substring(1)).toString().trim();
        Map root = new HashMap();
		root.put("packagepath", template.getPackagePath()==null||template.getPackagePath().trim().equals("")?"com.action":template.getPackagePath().trim());
        root.put("description", template.getDescription());
        root.put("classname", template.getClassName()==null||template.getClassName().trim().equals("")?BeanName.trim()+"Action":template.getClassName());
        root.put("listname", template.getListname()==null||template.getListname().trim().equals("")?template.getObjectName().trim()+"List":template.getListname().trim());
        root.put("beanname", BeanName.trim());
        root.put("beanobject", template.getObjectName());
        root.put("objectname", template.getObjectName());
        
        root.put("xmlname", template.getXmlname()==null||template.getXmlname().trim().equals("")?template.getObjectName()+".xml":template.getXmlname().trim());
        root.put("namespace", template.getNamespace()==null||template.getNamespace().trim().equals("")?template.getObjectName():template.getNamespace().trim());
        root.put("name", template.getName()==null||template.getName().trim().equals("")?template.getObjectName():template.getName().trim());
        root.put("actionname", template.getActionname()==null||template.getActionname().trim().equals("")?BeanName.trim()+"Action":template.getActionname().trim());
        root.put("classpath", template.getClasspath()==null||template.getClasspath().trim().equals("")?template.getObjectName().trim()+"action":template.getClasspath().trim());
//        root.put("jspname", template.getJspname());
        root.put("jspname", template.getObjectName());
        root.put("extends", template.getExtendsPackage()==null||template.getExtendsPackage().trim().equals("")?"struts-default":template.getExtendsPackage().trim());
        root.put("tablename", template.getTableName()==null||template.getTableName().trim().equals("")?"fj_"+template.getObjectName()+"":template.getTableName().trim());
        try {
        	String targetFile="";
        	String targetFile_struts_xml="";
        	String service_targetFile="";
        	String dao_targetFile="";
        	String serviceimp_targetFile="";
        	String daoimp_targetFile="";
        	String sqlmap_targetFile="";
        	String add_jsp_targetFile="";
        	String edit_jsp_targetFile="";
        	String list_jsp_targetFile="";
        	
        	if( !action.equals("") && action.equals("1")){
	            //生成action类
	            String sourceFile = "ActionTemplate.ftl";
	            targetFile = template.getClassName()==null||template.getClassName().trim().equals("")?BeanName.trim()+"Action.java":template.getClassName().trim()+".java";
	            FreeMarkerHelper freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("action.template.source.path"));
	//			freemarker.createStaticPage(root, sourceFile, request.getRealPath("/")+this.getPropertiesValue("config","action.template.target.path")+ "/", targetFile);
	            freemarker.createStaticPage(root, sourceFile, this.getMyConfigPropertyValue("src")+this.getConfigPropertyValue("action.template.target.path"), targetFile);
        	}
	        if( !struts_xml.equals("") && struts_xml.equals("2")){
	            //生成struts的xml
	            String sourceFile_struts_xml = "template.xml";
	            targetFile_struts_xml = template.getXmlname()==null||template.getXmlname().trim().equals("")?template.getObjectName().trim()+".xml":template.getXmlname().trim();
	            FreeMarkerHelper freemarker_xml = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("struts.xml.template.source.path"));
	            freemarker_xml.createStaticPage(root, sourceFile_struts_xml, this.getMyConfigPropertyValue("src")+this.getConfigPropertyValue("struts.xml.template.target.path"), targetFile_struts_xml);
        	}

            
	        if( !service.equals("") && service.equals("3")){
	//          生成service接口
	            String service_sourceFile = "TemplateService.ftl";
	            service_targetFile = "I"+BeanName.trim()+"Service.java";
	            FreeMarkerHelper service_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("service.template.source.path"));
	            service_freemarker.createStaticPage(root, service_sourceFile, this.getMyConfigPropertyValue("src")+this.getConfigPropertyValue("service.template.target.path"), service_targetFile);
	        }
	        if( !dao.equals("") && dao.equals("4")){
	//          生成dao接口
	            String dao_sourceFile = "TemplateDao.ftl";
	            dao_targetFile = "I"+BeanName.trim()+"DAO.java";
	            FreeMarkerHelper dao_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("dao.template.source.path"));
	            dao_freemarker.createStaticPage(root, dao_sourceFile, this.getMyConfigPropertyValue("src")+this.getConfigPropertyValue("dao.template.target.path"), dao_targetFile);
	        }
	        if( !serviceimp.equals("") && serviceimp.equals("5")){
	//          生成serviceimp实现类
	            String serviceimp_sourceFile = "TemplateServiceImp.ftl";
	            serviceimp_targetFile = BeanName.trim()+"ServiceImp.java";
	            FreeMarkerHelper serviceimp_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("serviceimp.template.source.path"));
	            serviceimp_freemarker.createStaticPage(root, serviceimp_sourceFile, this.getMyConfigPropertyValue("src")+this.getConfigPropertyValue("serviceimp.template.target.path"), serviceimp_targetFile);
	        }
	        if( !daoimp.equals("") && daoimp.equals("6")){
	//          生成daoimp实现类
	            String daoimp_sourceFile = "TemplateDaoImp.ftl";
	            daoimp_targetFile = BeanName.trim()+"DAOImp.java";
	            FreeMarkerHelper daoimp_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("daoimp.template.source.path"));
	            daoimp_freemarker.createStaticPage(root, daoimp_sourceFile, this.getMyConfigPropertyValue("src")+this.getConfigPropertyValue("daoimp.template.target.path"), daoimp_targetFile);
	        }
	        if( !sqlmap_xml.equals("") && sqlmap_xml.equals("7")){
	        	//生成sqlmap xml
	            String sqlmap_sourceFile = "template-map.xml";
	            sqlmap_targetFile = BeanName.trim()+"-Map.xml";
	            FreeMarkerHelper sqlmap_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("sqlmap.template.source.path"));
	            sqlmap_freemarker.createStaticPage(root, sqlmap_sourceFile, this.getMyConfigPropertyValue("src")+this.getConfigPropertyValue("sqlmap.template.target.path"), sqlmap_targetFile);
	        }
	        if( !add_jsp.equals("") && add_jsp.equals("8")){
	        	//生成add jsp
	            String add_jsp_sourceFile = "add_jsp_template.ftl";
	            add_jsp_targetFile = "add_"+template.getObjectName().trim()+".jsp";
	            FreeMarkerHelper add_jsp_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("jsp.template.source.path"));
	            add_jsp_freemarker.createStaticPage(root, add_jsp_sourceFile, this.getMyConfigPropertyValue("webroot")+this.getConfigPropertyValue("jsp.template.target.path")+template.getObjectName().trim()+"/", add_jsp_targetFile);
	        }
	        if( !edit_jsp.equals("") && edit_jsp.equals("9")){
	        	//生成edit jsp
	            String edit_jsp_sourceFile = "edit_jsp_template.ftl";
	            edit_jsp_targetFile = "edit_"+template.getObjectName().trim()+".jsp";
	            FreeMarkerHelper edit_jsp_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("jsp.template.source.path"));
	            edit_jsp_freemarker.createStaticPage(root, edit_jsp_sourceFile, this.getMyConfigPropertyValue("webroot")+this.getConfigPropertyValue("jsp.template.target.path")+template.getObjectName().trim()+"/", edit_jsp_targetFile);
	        }
	        if( !list_jsp.equals("") && list_jsp.equals("10")){
	        	//生成list jsp
	            String list_jsp_sourceFile = "list_jsp_template.ftl";
	            list_jsp_targetFile = "list_"+template.getObjectName().trim()+".jsp";
	            FreeMarkerHelper list_jsp_freemarker = new FreeMarkerHelper(this.getRealyPath("/")+this.getConfigPropertyValue("jsp.template.source.path"));
	            list_jsp_freemarker.createStaticPage(root, list_jsp_sourceFile, this.getMyConfigPropertyValue("webroot")+this.getConfigPropertyValue("jsp.template.target.path")+template.getObjectName().trim()+"/", list_jsp_targetFile);
	        }
	        
	        if( !struts_xml.equals("") && struts_xml.equals("2")){
		        //把生成的struts  xml文件加入到struts.xml(必须可写)中
		        Struts struts = new Struts();
		        struts.setFile("com/xml/"+targetFile_struts_xml);
				XMLParse xmlp2 = new XMLParse("","struts.xml");
				xmlp2.setEncoding("utf-8");
				try {
					xmlp2.addStrutsXml(struts);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		        //把生成的sqlmap xml文件加入到sqlMapConfig.xml(必须可写) 
		        Sqlmap sqlmap = new Sqlmap();
				sqlmap.setResource("com/sqlmap/"+sqlmap_targetFile);
				XMLParse.src=this.getMyConfigPropertyValue("webroot");
				XMLParse xmlp = new XMLParse("WEB-INF","sqlMapConfig.xml");
				xmlp.setEncoding("utf-8");
				try {
					xmlp.addSqlmapXml(sqlmap);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
	        }
			
	        //在daobean中加入节点
	        if( !daobeanXml.equals("") && daobeanXml.equals("11")){
				Daobean daobean = new Daobean();
				daobean.setId(template.getObjectName()+"action");
				daobean.setClasspath("com.action."+BeanName+"Action");
				daobean.setPropertyName("I"+BeanName);
				daobean.setRef(template.getObjectName()+"imp");
				
				daobean.setDaoid(template.getObjectName()+"DAO");
				daobean.setDaoclass("com.implementsfaces.dao."+BeanName+"DAOImp");
				daobean.setDaoparent("baseDao");
				
				daobean.setServiceid(template.getObjectName()+"imp");
				daobean.setServiceclass("com.implementsfaces.service."+BeanName+"ServiceImp");
				daobean.setServicePropertyName(template.getObjectName()+"DAO");
				daobean.setServiceRef(template.getObjectName()+"DAO");
				
				
				XMLParse.src=ConfigUtil.getMyConfigPropertyValue("src");
				XMLParse xmlp3 = new XMLParse("","daoBean_creator.xml");
				xmlp3.setEncoding("utf-8");
				try {
					xmlp3.addDaobeanXml(daobean);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
			
			
			
            Function.alertAndBack("恭喜！" + targetFile+","
					+ ", " + targetFile_struts_xml + "," + service_targetFile +","+dao_targetFile+","+serviceimp_targetFile+","+daoimp_targetFile+","+sqlmap_targetFile+","+add_jsp_targetFile+","+edit_jsp_targetFile+","+list_jsp_targetFile
					+ "  生成成功！", -1, this.getRequest(), this.getResponse(),
					null);
            root.clear();
		} catch (Exception ex) {
			logger.error("生成文件发生异常！", ex);
//			this.addActionError("生成文件发生异常！");
			return ERROR;
		}
        return null;
    }
    
    public void createFile(String sourceFile,String targetFile){
    	
    }

    public String getSourceFile() {
        return sourceFile;
    }

    public void setSourceFile(String sourceFile) {
        this.sourceFile = sourceFile;
    }

    public String getTargetFile() {
        return targetFile;
    }

    public void setTargetFile(String targetFile) {
        this.targetFile = targetFile;
    }

    public Template getTemplate() {
        return template;
    }

    public void setTemplate(Template template) {
        this.template = template;
    }

	public List getIds() {
		return ids;
	}

	public void setIds(List ids) {
		this.ids = ids;
	}

	public List getBeanList() {
		return beanList;
	}

	public void setBeanList(List beanList) {
		this.beanList = beanList;
	}
    
    
}
